coin = int(input("Введите четырёхзначное натуральное число: "))
if not 1000 <= coin <= 9999:
    print(0)
else:
    max_prime = 0
    coin_str = str(coin)
    for i in range(4, 0, -1):  
        for j in range(0, 5 - i):
            sub_coin = int(coin_str[j:j+i])
            if sub_coin > 1:
                is_prime = True
                for k in range(2, int(sub_coin**0.5) + 1):
                    if sub_coin % k == 0:
                        is_prime = False
                        break
                if is_prime and sub_coin > max_prime:
                    max_prime = sub_coin
    print(max_prime if max_prime else 0)
